//用户管理模块

var async=require('async');
var Shop=require('../../model/Shop');


//商家管理首页
exports.list=function(req,res){
	var data={};
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;

	//获取用户列表
	Shop.all(function(err,shopes){
		data["shopes"]=[];
		if(shopes){
			for(var i=0;i<shopes.length;i++){
				data["shopes"].push(JSON.parse(shopes[i]));
			}
		}
		console.log("data:"+JSON.stringify(data));
		res.render('admin/shop/overview',data);
	})
}

//商家创建页面
exports.createView=function(req,res){
	var data={};
	
	//用户数据
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;
	//标识是否是编辑页面
	data["edit"]=false;
	res.render('admin/shop/create_edit',data);
}

//商家创建操作
exports.create=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var name=req.body.name.trim();

	var shop=new Shop(name);

	//插入数据
	shop.insert(function(err){
		if(err) {
			console.log("插入数据失败");
			req.session.operate={"success":false,"msg":"添加商家信息失败!"};
			res.redirect('/admin/shop/create');
		}else{
			console.log("插入数据成功");
			req.session.operate={"success":true,"msg":"已成功添加商家信息!"};
			res.redirect('/admin/shop');
		}
	})
}


//商家编辑页面
exports.editView=function(req,res){
	var shopId=parseInt(req.params.id);

	//获取商家信息
	var getShopInfoById=function(callback){
		Shop.findById(shopId,function(err,shop){
			if(err) return callback(err);
			var data={};
			console.log("shop:"+shop);
			data["editShop"]=JSON.parse(shop);
			callback(null,data);
		})
	}

	async.waterfall([getShopInfoById],function(err,data){
		if(err) return res.send('数据错误');
		
		//用户数据
		var name=req.session.user.name||'admin';
		var user={
			name:name,
		}
		data["user"]=user;
		
		//操作提醒
		var operate={"success":false,"msg":""};
		if(req.session&&req.session.operate!=null) {
			operate=req.session.operate;
			delete req.session.operate;
		}
		data["operate"]=operate;
		//标识是否是编辑页面
		data["edit"]=true;
		console.log("data:"+JSON.stringify(data));
		res.render('admin/shop/create_edit',data);
	});
}

//商家编辑操作
exports.edit=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var shopId=parseInt(req.params.id);
	var name=req.body.name.trim();

	var shop=new Shop(name);

	shop.update(shopId,function(err){
		if(err) {
			console.log("更新数据失败");
		    req.session.operate={"success":false,"msg":"更新商家信息失败!"};
			res.redirect('/admin/shop/edit/'+shopId);
		}else{
			console.log("更新数据成功");
			req.session.operate={"success":true,"msg":"已成功保存商家信息!"};
			res.redirect('/admin/shop');
		}
    })
}


//商家删除页面
exports.deleteView=function(req,res){
	var shopId=parseInt(req.params.id);

	//获取商家信息
	var getShopInfoById=function(callback){
		Shop.findById(shopId,function(err,shop){
			if(err) return callback(err);
			var data={};
			console.log("shop:"+shop);
			data["editShop"]=JSON.parse(shop);
			callback(null,data);
		})
	}

	async.waterfall([getShopInfoById],function(err,data){
		if(err) return res.send('数据错误');

		//用户数据
		var name=req.session.user.name||'admin';
		var user={
			name:name
		}
		data["user"]=user;

		//操作提醒
		var operate={"success":false,"msg":""};
		if(req.session&&req.session.operate!=null) {
			operate=req.session.operate;
			delete req.session.operate;
		}
		data["operate"]=operate;
		res.render('admin/shop/delete',data);
	});

}

//商家删除操作
exports.delete=function(req,res){
	var shopId=parseInt(req.params.id);

	Shop.deleteById(shopId,function(err){
		if(err) {
			console.log("删除数据失败");
			req.session.operate={"success":false,"msg":"删除商家信息失败!"};
			res.redirect('/admin/shop/delete/'+shopId);
		}else{
			console.log("删除数据成功");
			req.session.operate={"success":true,"msg":"删除商家信息成功!"};
			res.redirect('/admin/shop');
		}	
	})
}



